﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
using ZYHand.IService;
using ZYHand.Repository;

namespace ZYHand.Service
{
    public class MongoDBService: IMongoDBService
    { 

        public MongoDBService() { }
        public int AddModelbyString(string tableName, string json)
        {
            var Result = MongodbHelper<ExpandoObject>.AddbyString(new MongodbHost { Table = tableName }, json);
            return Result;
        }

        public long DeleteMany(string tableName, FilterDefinition<ExpandoObject> filter)
        {
            var Result = MongodbHelper<ExpandoObject>.DeleteMany(new MongodbHost { Table = tableName }, filter);
            return Result.DeletedCount;
        }

        public long UpdateManay(string tableName, Dictionary<string, string> dic, FilterDefinition<ExpandoObject> filter)
        {
            var Result = MongodbHelper<ExpandoObject>.UpdateManay(new MongodbHost { Table = tableName },dic, filter);
            return Result.MatchedCount;
        }






        public int AddModel<T>(string tableName,T data) where T:class,new ()
        {
            var Result = MongodbHelper<T>.Add(new MongodbHost { Table = tableName }, data);
            return Result;
        }

        public List<T> FindList<T>(string tableName, FilterDefinition<T> filter, string[] field = null, SortDefinition<T> sort = null) where T : class, new()
        {
            var Result = MongodbHelper<T>.FindList(new MongodbHost { Table = tableName }, filter, field, sort);
            return Result;
        }

        public  List<T> FindListByPage<T>(string tableName, FilterDefinition<T> filter, int pageIndex, int pageSize, out long count, string[] field = null, SortDefinition<T> sort = null) where T : class, new()
        {

            var Result = MongodbHelper<T>.FindListByPage(new MongodbHost { Table = tableName }, filter, pageIndex, pageSize, out count, field, sort);
           //count = count;
            return Result;
        }
    }
}
